/* 
 * UrlyBirdMainView.java 19.06.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.ps;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

/**
 * Main entrance Window. Filepath is chooseable.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class UrlyBirdMainView extends JFrame {

    private static final long serialVersionUID = 1L;

    private ApplicationRunMode mode;

    private final int iw = 4;
    private final Insets formPanelInsets = new Insets(iw, iw, iw, iw);
    private GridBagConstraints gbc = new GridBagConstraints();

    private JButton jbtServerStart;
    private JButton jbtOk;
    private JButton jbtCancel;
    private JButton jbtPath;

    private JLabel jlbFileName;
    private JTextField jtxtFileName;
    private JLabel jlbPort;
    private JTextField jtxtPort;
    private JLabel jlbIp;
    private JTextField jtxtIp;

    private JPanel panelInput;
    private JPanel panelButtons;

    private JFileChooser fc = new JFileChooser();

    public UrlyBirdMainView() {
	super("Urly Bird");
	startGui();
    }

    public UrlyBirdMainView(ApplicationRunMode mode) {
	super("Urly Bird");
	this.mode = mode;
	startGui();
    }

    /**
     * Calls methods to create the gui and to initialize the window.
     */
    public void startGui() {
	createGui();
	init();
    }

    /**
     * Initializes all the default window behaviours
     * 
     * @param args
     */
    private void init() {

	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.pack();
	this.setSize(500, 180);

	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (int) ((dim.getWidth() - this.getWidth()) / 2);
	int y = (int) ((dim.getHeight() - this.getHeight()) / 2);
	this.setLocation(x, y);
	this.setResizable(false);
	this.setVisible(true);
    }

    /**
     * Create the GUI and show it.
     * 
     */
    public void createGui() {

	// Layout definition
	defineLayout();

	// Buttons
	panelButtons = new JPanel();
	panelButtons.setLayout(new GridBagLayout());

	if (ApplicationRunMode.NETWORK_CLIENT.equals(mode)) {
	    createPortPanel();
	    createIpPanel();
	    createOkButton();
	} else if (ApplicationRunMode.SERVER.equals(mode)) {
	    createFilePathPanel();
	    createPortPanel();
	    createStartServerButton();
	} else if (ApplicationRunMode.STANDALONE_CLIENT.equals(mode)) {
	    createFilePathPanel();
	    createOkButton();
	}

	createCancelButton();

	this.add(panelInput, BorderLayout.NORTH);
	this.add(panelButtons, BorderLayout.SOUTH);
    }

    /**
     * Defines the overall layout
     */
    public void defineLayout() {
	gbc.insets = formPanelInsets;
	gbc.fill = GridBagConstraints.BOTH;

	Border etchedBorder = new EtchedBorder();
	Border emptyBorder = new EmptyBorder(9, 9, 9, 9);
	Border panelBorder = BorderFactory.createCompoundBorder(emptyBorder,
		etchedBorder);

	// Initialize panel for input fields
	panelInput = new JPanel();
	panelInput.setLayout(new GridBagLayout());
	panelInput.setBorder(panelBorder);

    }

    /**
     * Creates the panel for the file path
     */
    public void createFilePathPanel() {
	jlbFileName = new JLabel("Filename with path: ");
	gbc.gridx = 0;
	gbc.gridy = 0;
	panelInput.add(jlbFileName, gbc);

	jtxtFileName = new JTextField();
	jtxtFileName.setColumns(15);
	jtxtFileName.setText(GuiHelper.getPropertyPath());
	jtxtFileName.setEditable(false);

	gbc.gridx = 1;
	gbc.gridy = 0;
	panelInput.add(jtxtFileName, gbc);

	jbtPath = new JButton("Choose File");
	gbc.gridx = 2;
	gbc.gridy = 0;
	panelInput.add(jbtPath, gbc);
    }

    /**
     * Creates the cancel button
     */
    public void createCancelButton() {
	jbtCancel = new JButton("Cancel");
	gbc.gridx = 2;
	gbc.gridy = 1;
	panelButtons.add(jbtCancel, gbc);
    }

    /**
     * Creates the button to start the server
     */
    public void createStartServerButton() {
	jbtServerStart = new JButton("Start Server");
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelButtons.add(jbtServerStart, gbc);
    }

    /**
     * Creates the submit button
     */
    public void createOkButton() {
	jbtOk = new JButton("Ok");
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelButtons.add(jbtOk, gbc);
    }

    /**
     * creates the panel for the ip address input
     */
    public void createIpPanel() {
	// Ip
	jlbIp = new JLabel("IP: ");
	gbc.gridx = 0;
	gbc.gridy = 2;
	panelInput.add(jlbIp, gbc);

	jtxtIp = new JTextField();
	jtxtIp.setColumns(15);
	jtxtIp.setText(GuiHelper.getPropertyIp());
	
	gbc.gridx = 1;
	gbc.gridy = 2;
	panelInput.add(jtxtIp, gbc);
    }

    /**
     * creates the panel for the port input
     */
    public void createPortPanel() {
	// Port
	jlbPort = new JLabel("Port: ");
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelInput.add(jlbPort, gbc);

	jtxtPort = new JTextField();
	jtxtPort.setColumns(15);
	jtxtPort.setText(GuiHelper.getPropertyPort());
	gbc.gridx = 1;
	gbc.gridy = 1;
	panelInput.add(jtxtPort, gbc);
    }

    // Functions for the gui controller

    /**
     * Sets the given file to the file chooser
     * 
     * @param file
     */
    public void setSelectedFile(File file) {
	this.fc.setSelectedFile(file);
    }

    /**
     * Returns the file chooser of the view
     * 
     * @return file chooser
     */
    public JFileChooser getFileChooser() {
	return this.fc;
    }

    /**
     * Returns the entered filename
     * 
     * @return filename
     */
    public String getTxtFileName() {
	return this.jtxtFileName.getText();
    }

    /**
     * Returns the entered ip address
     * 
     * @return ip address
     */
    public String getTxtIp() {
	return this.jtxtIp.getText();
    }

    /**
     * Returns the entered port number
     * 
     * @return port number
     */
    public String getTxtPort() {
	return this.jtxtPort.getText();
    }

    /**
     * Sets the given filename to the filename label in the view
     * 
     * @param txtFilename
     *            filename
     */
    public void setTxtFileName(String txtFilename) {
	this.jtxtFileName.setText(txtFilename);
    }

    /**
     * Returns the server start button
     * 
     * @return server start button
     */
    public JButton getButtonServer() {
	return this.jbtServerStart;
    }

    /**
     * Returns the ok button
     * 
     * @return ok button
     */
    public JButton getButtonOk() {
	return this.jbtOk;
    }

    /**
     * Function for the controller: adds FileChooserListener to Path-Button
     */
    public void setFileChooserListener(ActionListener fileChooserListener) {

	if (this.jbtPath != null) {
	    this.jbtPath.addActionListener(fileChooserListener);
	}
    }

    /**
     * Function for the controller: adds OkListener to ok-button
     */
    public void setOkListener(ActionListener okListener) {
	if (this.jbtOk != null) {
	    this.jbtOk.addActionListener(okListener);
	}
    }

    /**
     * Function for the controller: adds ServerStartListener to
     * serverstart-button
     */
    public void setServerStartListener(ActionListener serverStartListener) {
	if (this.jbtServerStart != null) {
	    this.jbtServerStart.addActionListener(serverStartListener);
	}
    }

    /**
     * Function for the controller: adds ExitListener to cancel-button
     */
    public void setExitListener(ActionListener exitListener) {
	this.jbtCancel.addActionListener(exitListener);
    }
}
